/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._342;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 *
 * 给定一个整数 (32 位有符号整数)，请编写一个函数来判断它是否是 4 的幂次方。
 *
 * 示例 1:
 *
 * 输入: 16
 * 输出: true
 * 示例 2:
 *
 * 输入: 5
 * 输出: false
 * 进阶：
 * 你能不使用循环或者递归来完成本题吗？
 *
 *
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._342
 * @ClassName: PowerOfFour
 * @Author: linyaonan
 * @Date: 2019/12/23 10:07
 */
public class PowerOfFour {

    public static boolean isPowerOfFour(int num) {

        if (num <= 0) {
            return false;
        }
        if (0 == (num & (num-1))) {
            while (num >= 4) {
                num >>= 2;
            }
            return num == 1;
        } else {
            return false;
        }
    }

    public static boolean isPowerOfFour2(int num) {

        if (num <= 0) {
            return false;
        }
        return num == 1 || num == 4 || num == 16 || num == 64 || num == 256 || num == 1024
                || num == 4096 || num == 16384 || num == 65536 || num == 262144 || num == 1048576
                || num == 4194304 || num == 16777216 || num == 67108864 || num == 268435456 || num == 1073741824;
    }


    public static void main(String[] args) {

        int count = 0;
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            boolean powerOfFour = isPowerOfFour(i);
            if (powerOfFour) {
                System.out.println(i + "---->>>>>" + count++);
            }
        }
    }

    private static int[] getList() {
        return new int[] {1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,
                16777216,67108864,268435456,1073741824};
    }

}
